<template>
  <div class="login-page">
    <el-row class="login-container">
      <el-col class="login-left" :span="12" :xs="0">
        <!-- <div class="login-bg"></div> -->
        <img
          class="login-left-img"
          src="@/assets/imgs/login_left.png"
          alt="login_left"
        />
      </el-col>
      <el-col class="login-right" :span="12" :xs="24">
        <div class="login-main">
          <h2 class="login-title">Vue3+TS后台管理模板</h2>
          <el-form
            class="demo-form-inline"
            ref="loginForm"
            :model="formObj"
            :rules="formRules"
          >
            <el-form-item label="账号" prop="username">
              <el-input
                v-model="formObj.username"
                placeholder="请输入账号"
                clearable
              />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                v-model="formObj.password"
                type="password"
                placeholder="请输入密码"
                autocomplete="off"
                clearable
                @keyup.enter="submitForm(loginForm)"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                class="login-btn"
                type="primary"
                @click="submitForm(loginForm)"
              >
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import useUserStore from '@/store/modules/user'
import { FormInstance, FormRules } from 'element-plus'
import { useRouter } from 'vue-router'

const userStore = useUserStore()
const router = useRouter()

const formObj = reactive({
  username: 'admin',
  password: '111111'
})
const formRules = reactive<FormRules>({
  username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
})

// 登录
const postLoginFun = async () => {
  await userStore.postLoginAction(formObj)
  router.push({ path: '/home' })
}

const loginForm = ref<FormInstance>()
// 提交登录
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid) => {
    if (valid) {
      postLoginFun()
    }
  })
}
</script>

<style lang="scss" scoped>
.login-page {
  width: 100%;
  height: 100%;
  background: url('@/assets/imgs/login_bg.jpg') no-repeat;
  background-size: cover;
}

.login-container {
  height: 100%;
}

.login-bg {
  width: 100%;
  height: 100%;
}

.login-left {
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-left-img {
  max-width: 90%;
}

.login-right {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;

  .login-title {
    line-height: 32px;
    margin: 20px 0 30px;
    font-size: 26px;
    text-align: center;
  }

  .login-btn {
    width: 100%;
    margin-top: 15px;
  }
}

.login-main {
  width: 70%;
  max-width: 500px;
  padding: 20px 40px 15px;
  color: #fff;
  background-color: rgba(0, 0, 0, 0.2);
  border-radius: 4px;

  :deep(.el-form-item__label) {
    color: #fff;
  }
}
</style>
